var flag=0;
$(function(){
	$('#addBtn').click(function(){
		addInfo();
	});
	//保存事件
	$('.btn-add').click(function(){
		saveInfo();
	});
});

//增加<tr/>
function addInfo(){
	var startNum = $('#startNumId').val();
	var endNum = $('#endNumId').val();
	if(startNum =='' && endNum ==''){
		layer.msg('题号范围不能为空！');
	}
	if(startNum ==''){
		startNum = endNum;
	}
	if(endNum ==''){
		endNum = startNum;
	}
	//checkNumberFormat(startNum,'startNumId',0,'开始题号');
	//if(flag==1)return;
	//checkNumberFormat(endNum,'endNumId',0,'结束题号');
	//if(flag==1)return;
	//判断开始数字是否大于结束数字
	if(parseInt(startNum) > parseInt(endNum)){
		layer.msg('开始题号不能大于结束编号！');
	}
	endNum = parseInt(endNum);
	startNum = parseInt(startNum);
	
	//判断题号是否重复
	//循环页面题号是否重复
	if($('.input_th').length==0){
		for(var i=startNum;i<=endNum;i++){
			addRow(i);
		}
	}else{
		var ths='@';
		$('.input_th').each(function () {
			var th = $(this).val().trim();
			ths += th+'@';
		});
		for(var i=startNum;i<=endNum;i++){
			var th_ = '@'+i+'@';
			if(ths.indexOf(th_)==-1){
				addRow(parseInt(i));
			}
		}
	}
	$('#startNumId').val('');
	$('#endNumId').val('');
}

//删除<tr/>
var deltr =function($this,recordId,type) {
	var title='提示信息';
	var succ='删除成功!';
	var fail='删除失败!';
	if(type=='0'){//新增数据库未存储的记录
		$this.parent().parent().remove();//删除当前行
	}else{//编辑数据库已存储的记录
		common_ajax.ajax(rootPath+'/manage/courseObjectiveAnswer/delete/exec', 
		{
			id:recordId
		}, 'json', function(data){
			if(data.resultCode == '000000'){
				$this.parent().parent().remove();//删除当前行
				layer.msg('操作成功！');
			}else{
				layer.msg('操作失败！');
				return false;
			}
		});
	}
}

function addRow(th){
	var recordId = common_common.randomChar();
	$("#dataTable tbody ").append(
       	"<tr id='"+recordId+"_0' align='center'>"
            +"<td><input type='text' disabled class='input_th' rid='"+recordId+"' id='th_"+recordId+"' name='th_"+recordId+"' value='"+th+"' style='width:100%;'/></td>"
            +"<td><input type='text' class='input_check input_da' rid='"+recordId+"' id='da_"+recordId+"' name='da_"+recordId+"' placeholder='输入题目答案' style='width:90%;' /></td>"
            +"<td><a href='#' class='btn btn-danger btn-sm btn-delete' onclick='deltr($(this),\""+recordId+"\",0)'>删除</a></td>"
        +"</tr>");
}

/*function checkAllExists(value,inputName,recordId,type,inputTitle){
	var id = inputName+recordId;
	checkNumberFormat(value,id,type,inputTitle);
	if(flag==1)return;
	//题号校验数字是否存在
	if(type==0){
		//循环页面题号是否重复
		$('.input_th').each(function () {
			var id1 = $(this).attr("id");
			if(id1!=id){
				var val = $(this).val().trim();
				if(value==val){
					checkNumberFormat(value,id,2,inputTitle);
				}
			}
		});
	}
}*/

//焦点离开校验
function checkNumberFormat(value,inputid,type,inputTitle){
	var reg_float = /^[0-9]*$/;//格式：匹配数字/^[0-9]*$/
	var reg_char = /^[A-Za-z]+$/;//格式：匹配由26个英文字母组成的字符串
	//输入值空值判断
	var msg1= '不能为空！';
	var msg2= '只能输入整数数字！';
	var msg3= '已存在该题号，请更换！';
	var msg4= '只能输入字母，不能包含数字或其他特殊字符！';
	if(value ==''){
		$('#'+inputid).focus();
		flag=1;
		layer.msg(inputTitle+msg1);
		return false;
	}
	//输入值校验合法性
	if(type==0){
		if(!reg_float.test(value)){
	    	$('#'+inputid).val('').focus();
	    	flag=1;
	    	layer.msg(inputTitle+msg2);
	   		return false; 
	  	}
	}else if(type==1){//校验答案只能输入字母
		if(!reg_char.test(value)){
			$('#'+inputid).val('').focus();
	    	flag=1;
	    	layer.msg(inputTitle+msg4);
	   		return false; 
		}
	}else if(type==2){//判断是否重复
    	$('#'+inputid).val('').focus();
    	flag=1;
    	layer.msg(inputTitle+msg3);
   		return false; 
	}
}

//保存事件
function saveInfo(){
	var strs=[],ids=[];
	$('.input_th').each(function () {
		var id1 = $(this).attr("id");
		var rid = $(this).attr("rid");
		var th_val = $(this).val();
		var da_val = $('#da_'+rid).val();
		//checkNumberFormat(th_val,id1,0,'题号');
		//if(flag==1)return;
		checkNumberFormat(da_val,'da_'+rid,1,'答案');
		if(flag==1)return;
		ids.push(rid);
		strs.push(rid+'#'+th_val+'#'+da_val);
	});
	if(strs!=''){
		common_ajax.ajax(rootPath+'/manage/courseObjectiveAnswer/edit/exec', 
		{
			ids:ids.join(','),
			strs:strs.join(','),
			owner:owner
		}, 'json', function(data){
			if(data.resultCode == '000000'){
				layer.msg('操作成功！');
			}else{
				layer.msg('操作失败！');
				return false;
			}
		});
	}
	
	setTimeout(function () {
		location.href = rootPath+'/manage/courseObjectiveAnswer?owner='+owner;
    }, 1000);
}
